﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECommerce.Library;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using System.Text;
using System.Web.UI.HtmlControls;
using Entity;
using eShop.Model;
using eShop.DataAccess;
using eShop.Business;

public partial class controls_wucProductCategoryListingHome : System.Web.UI.UserControl
{
    public String ProductPath
    {
        get
        {
            try
            {
                return Page.RouteData.Values["path"].ToString();
            }
            catch
            {
                return string.Empty;
            }
        }
    }

    public int RepeatedColumns
    {
        get { return _repeatedColumns; }
        set { _repeatedColumns = value; }
    }

    public int MaxItem
    {
        get { return _maxItem; }
        set { _maxItem = value; }
    }

    protected StringBuilder MyData = new StringBuilder();
    private int _repeatedColumns = 3;
    private int _maxItem = 9;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void LoadListing(string catName)
    {
        LoadRepeaterItems(catName);
    }

    protected void LoadRepeaterItems(string catName)
    {
        Category cat = CategoryManager.GetByCategoryUrl(catName);
        if (cat != null)
        {
            imgCategoryBanner.ImageUrl = cat.CategoryBannerImageUrl;
        }
        CustomCollection<Product> product = Product.GetProductByCategoryURL(catName);
        if (product != null)
        {
            rptHorizontal.DataSource = product;
            rptHorizontal.DataBind();
        }
    }



    protected void Horizontal_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Product product = (Product)e.Item.DataItem;
                string[] words = product.ProductUrl.Split('/');
                string lasturl = string.Empty;
                foreach (string word in words)
                {
                    if (word.Contains(".aspx"))
                    {
                        lasturl = word.ToString();
                    }
                }
                // product image
                Image imgProduct = (Image)e.Item.FindControl("imgProduct");
                System.Drawing.Size originalImageDimension = ImageResizer.GetImageDimension(product.ProductImageSmallThumbnail);
                if (originalImageDimension.Width > 150)
                {
                    imgProduct.Style.Add("width", "150px;");
                }

                imgProduct.ImageUrl = ImageResizer.CheckImageMissing(product.ProductImageSmallThumbnail, AutoGear.BLL.SiteSettings.NoImageSmallThumbnailUrl);
                string imgUrl = imgProduct.ImageUrl.Replace(SiteSettings.SiteBaseUrl, "~");
                System.Drawing.Size imageSize = ImageResizer.GetImageDimension(imgUrl);
                if (imageSize.Height > 120)
                {
                    Unit imageHeight = new Unit(120);
                    imgProduct.Height = imageHeight;
                }
                HyperLink hlkProductPictureLink = (HyperLink)e.Item.FindControl("hlkProductPictureLink");
                hlkProductPictureLink.NavigateUrl = product.ProductUrl;

                HyperLink hlkProductNameLink = (HyperLink)e.Item.FindControl("hlkProductNameLink");
                hlkProductNameLink.Text = product.ProductName;

                hlkProductNameLink.NavigateUrl = product.ProductUrl;

                Label lblProductName2 = (Label)e.Item.FindControl("lblProductName2");
                lblProductName2.Text = product.ProductName2;

                Label lblProductPrice = (Label)e.Item.FindControl("lblProductPrice");
                lblProductPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(product.ProductPrice);

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void rptTooltipContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Product p = (Product)e.Item.DataItem;

            if (p != null)
            {
                HtmlImage imgProductImage = (HtmlImage)e.Item.FindControl("imgProductImage");
                imgProductImage.Src = p.ProductImageDetailSize;
            }
        }
    }
}